package com.easpeed.elevator.utils;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;

import com.easpeed.comon.consts.CustomSetting;
import com.github.mikephil.charting.data.Entry;

import java.util.Arrays;
import java.util.Collections;

public class ChartUtil {

    public static float lambd = 1.467f;

    public static int pdjl = 70;

    public static float dividingColorValue = 100f;
    //
    public static int fixedLength = 20000;
    //当前固定宽度 后期可以改成动态宽度(数据超过宽度范围不显示)
    public static int fixedWidth = CustomSetting.getDvsPointNum();  // 指定Bitmap的宽度
    public static int fixedHeight = 120;   // 指定Bitmap的高度
    //HSV颜色空间
    // 红色范围调整
    static float[] lower_red = {0, 1.0f, 0.3f};     // 更深的浅红色
    static float[] upper_red = {30, 1.0f, 0.7f};    // 深红色，亮度降低

    // 蓝色范围调整
    static float[] lower_blue = {200, 0.7f, 0.6f};   // 更浅的浅蓝色
    static float[] upper_blue = {240, 1.0f, 0.9f};   // 深蓝色，亮度降低


    public static int dvsXToIndex(float distance) {
        return (int)((distance +pdjl)*lambd/3);
    }

    public static float dvsIndexToDistance(int index) {
        return index * (3 / ChartUtil.lambd) - pdjl;
    }

    public static Bitmap initWaterfallBitmap()
    {
        // 创建一个黑色的Bitmap
        Bitmap blackBitmap = Bitmap.createBitmap(fixedWidth, fixedHeight, Bitmap.Config.ARGB_8888);
        // 获取Canvas对象以便于我们画图
        Canvas canvas = new Canvas(blackBitmap);
        // 设置画笔颜色为黑色
        Paint paint = new Paint();
        //设置为白色
        paint.setColor(Color.WHITE);
        // 绘制一个黑色的矩形，覆盖整个Bitmap
        canvas.drawRect(0, 0, fixedWidth, fixedHeight, paint);
        // 完成绘制后，回收canvas资源
        canvas.setBitmap(null);
        return blackBitmap;
    }


    public static int ColorRules(int value) {
        int argbColor;

        // 调整分割颜色的值
        float adjustedDividingColorValue = dividingColorValue * 2f;

        if (value < dividingColorValue) {
            float ratio = value / dividingColorValue;

            // 从浅蓝到深蓝的过渡
            argbColor = Color.HSVToColor(255, new float[] {
                    lower_blue[0] + ratio * (upper_blue[0] - lower_blue[0]),

                    lower_blue[1] + ratio * (upper_blue[1] - lower_blue[1]), // 饱和度

                    lower_blue[2] + ratio * (upper_blue[2] - lower_blue[2]) // 明亮度
            });
        } else {
            // 添加红色到深红的过渡
            float overRatio = (value - dividingColorValue) / (adjustedDividingColorValue - dividingColorValue);
            if (overRatio > 1.0f) overRatio = 1.0f;

            // 从浅红到深红的过渡
            argbColor = Color.HSVToColor(255, new float[] {

                    lower_red[0] + overRatio * (upper_red[0] - lower_red[0]), // 红色的色相
                    lower_red[1] + overRatio * (upper_red[1] - lower_red[1]), // 饱和度
                    lower_red[2] + overRatio * (upper_red[2] - lower_red[2]), // 渐变亮度
            });
        }

        return argbColor;
    }

    public static int getColorFromValue(int value) {
        // 限制值的范围在 0 到 300 之间
        value = Math.max(0, Math.min(300, value));

        // 20 以下为白色
        if (value < 20) {
            return Color.rgb(255, 255, 255); // 白色
        }

        // 定义颜色数组
        int[][] greenToRedColors = {
                {0, 255, 0},   // 绿色
                {255, 0, 0}    // 红色
        };

        float ratio;
        int index, nextIndex;

        // 20 到 dividingColorValue 使用绿色到红色渐变
        if (value <= dividingColorValue) {
            ratio = (value - 20) / (float)(dividingColorValue - 20);
            index = 0;
            nextIndex = 1;
        } else { // dividingColorValue 到 300 使用红色渐变
            ratio = (value - dividingColorValue) / (float)(300 - dividingColorValue);
            index = 1;
            nextIndex = 1; // 继续使用红色
        }

        // 线性插值
        int r = (int) (greenToRedColors[index][0] * (1 - ratio) + greenToRedColors[nextIndex][0] * ratio);
        int g = (int) (greenToRedColors[index][1] * (1 - ratio) + greenToRedColors[nextIndex][1] * ratio);
        int b = (int) (greenToRedColors[index][2] * (1 - ratio) + greenToRedColors[nextIndex][2] * ratio);

        return Color.rgb(r, g, b);
    }



//    public static int ColorRules(int value)
//    {
//
//        int argbColor;
//        if(value<dividingColorValue)
//        {
//            float ratio = value*1.0f/dividingColorValue;
//            argbColor = Color.HSVToColor(255,new float[]
//                    {lower_blue[0] + ratio*(upper_blue[0]-lower_blue[0]),
//                     1.0f,
//                     1.0f});
//        }
//        else
//        {
//            argbColor = Color.RED;
//        }
//
//        return argbColor;
//    }


    static Bitmap newBitmap = Bitmap.createBitmap(fixedHeight/2, fixedHeight, Bitmap.Config.ARGB_8888);
    public static Bitmap newIntensityBitmap(int [] intensity)
    {
        Canvas canvas = new Canvas(newBitmap);
        Paint paint = new Paint();
        paint.setColor(Color.WHITE);
        canvas.drawRect(0, 0, fixedHeight/2, fixedHeight, paint);

        //划线
        paint.setColor(Color.RED); // 设置画笔颜色F
        paint.setStrokeWidth(3); // 设置线宽
        paint.setAntiAlias(true); // 抗锯齿
        paint.setStyle(Paint.Style.STROKE); // 设置为描边模式
        ;

        //最后增加的是最新的值
        for(int index=0;index< intensity.length-1;index++)
        {
            //如果有距离点本次不存在 不划线
            if(intensity[index]==-1 || intensity[index+1]==-1)
                continue;

            int startX = intensity[index];
            int startY = index;

            int stopX = intensity[index+1];
            int stopY = index+1;
            canvas.drawLine(startX, startY, stopX, stopY, paint); // 绘制线条

        }


        // 完成绘制后，回收canvas资源
        canvas.setBitmap(null);
        return newBitmap;
    }


}
